package com.template.app.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {

	/**
	 * dateStr2Long
	 * @param date
	 * @return
	 */
	public static Long dateStr2Long(String date,String pattern){
		if(date==null || "".equals(date))return null;
		if(pattern==null){
			pattern="yyyy-MM-dd";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
			try {
				Date d=sdf.parse(date);
				Long l=d.getTime();
				return l;
			} catch (ParseException e) {
				e.printStackTrace();
			}
		return null;
	}
	
	/**
	 * long2Str
	 * @param d
	 * @return
	 */
	public static String string2Date(Long  d){
		if(d==null)return null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String s=sdf.format(new Date(d));
		return s;
	}
	
	
	public static String date2String(Date date){
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");//格式化日期显示方式
		String sdate = sf.format(date);
		return sdate;
	}

	public static String date2TimeString(Date date){
		if(date==null)return null;
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//格式化日期显示方式
		String sdate = sf.format(date);
		return sdate;
	}
	
	public static Date string2Date(String dateTemp){
		if(dateTemp==null || "".equals(dateTemp))return null;
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");//格式化日期显示方式
		String year = dateTemp.substring(0, 4);
        String month = dateTemp.substring(5, 7);
        String day = dateTemp.substring(8, 10);
        String sdate = year+"-"+month+"-"+day;
        Date date = null;
		try {
			date = sf.parse(sdate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
		
	}
	

}
